import os
import xmmsclient.glib
from gtk import *
from model import MlibModel
from xmmsclient import XMMS
from xmmsclient.collections import *

class MLib(MlibModel):
    def __init__(self, xmms2):
        self.xmms2 = xmms2
        b = Builder()
        b.add_from_file("mlib.glade")
        MlibModel.__init__(self, self, b)
        b.connect_signals(self)


    def on_albumview_cursor_changed(self, treeview):
        selection = treeview.get_selection()
        model, iter = selection.get_selected()
        album, = model.get(iter, 0)
        self.model.title.clear()
        p, = model.get_path(iter)
        if p == 0:
            if self.model.artistfilter is not None:
                artist = self.model.artistfilter
                self.xmms2.coll_query_infos(
                    Match(
                        Universe(),
                        field="artist",
                        value=artist),
                    ["album"],
			        cb=self.model.add_to_titlestore)
                self.xmms2.coll_query_infos(
                    Match(
                        Universe(),
                        field="artist",
                        value=artist),
			        ["title"],
			        cb=self.model.add_to_titlestore)
                return
            else:
                self.xmms2.coll_query_infos(
                    Has(
                        Universe(),
                        field="artist"),
			        ["album"],
			        cb=self.model.add_to_titlestore)
                self.xmms2.coll_query_infos(
                    Has(
                        Universe(),
                        field="artist"),
			        ["title"],
			        cb=self.model.add_to_titlestore)
                return
        if self.model.artistfilter is not None:
            artist = self.model.artistfilter
            self.xmms2.coll_query_infos(
            Intersection(Universe(),
			    Match(
				    Universe(), 
				    field="album",
                    value=album), 
                Match(
                    Universe(),
                    field="artist",
                    value=artist)),
			["title"],
			cb=self.model.add_to_titlestore)
            return
        self.xmms2.coll_query_infos(
			Match(
				Universe(), 
				field="album",
                value=album), 
			["title"],
			cb=self.model.add_to_titlestore)

    def on_artistview_cursor_changed(self, treeview):
        selection = treeview.get_selection()
        model, iter = selection.get_selected()
        artist, =  model.get(iter, 0)
        self.model.album.clear()
        self.model.title.clear()
        p, = model.get_path(iter)
        if p == 0:
            self.xmms2.coll_query_infos(
	    		Has(
		    		Universe(), 
			    	field="artist"), 
			    ["album"],
			    cb=self.model.add_to_albumstore)
            self.xmms2.coll_query_infos(
			    Has(
				    Universe(), 
				    field="artist"), 
			    ["title"],
			    cb=self.model.add_to_titlestore)
            return
            
        self.xmms2.coll_query_infos(
			Match(
				Universe(), 
				field="artist",
                value=artist), 
			["album"],
			cb=self.model.add_to_albumstore)
        self.xmms2.coll_query_infos(
			Match(
				Universe(), 
				field="artist",
                value=artist), 
			["title"],
			cb=self.model.add_to_titlestore)
        

    def quit(self, *args):
        main_quit()

xmms2 = XMMS("mlib")
try:
    xmms2.connect(os.getenv("XMMS_PATH"))
except Exception, e:
    print "[xmms2 client]: Could not connect to xmms2d"
    print e
    print "Aborting!"
    exit(1)
xmmsclient.glib.GLibConnector(xmms2)

MLib(xmms2)
main()
